package com.zdkj.subway.st.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.subway.st.entity.PkProdparams;
import com.zdkj.subway.st.entity.SwStPlanDay;
import com.zdkj.subway.st.service.PkProdparamsService;
import com.zdkj.subway.st.service.SwStPlanDayService;
import com.zdkj.subway.st.util.DateUtilsSt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/swStProductPlanDay")
@Api(value = "swStProductPlanDay", tags = "生产日计划类型为2")
@Slf4j
@Inner(value = false)
public class SwStProductPlanDayController {
    private final SwStPlanDayService swStPlanDayService;
    private final PkProdparamsService pkProdparamsService;


    @ApiOperation(value = "生产日计划查询", notes = "根据type=2查询生产日计划")
    @GetMapping("/type")
    public R getProductPlanDay(@RequestParam(value = "type",required = true) String type) {
        if(!"2".equals(type)){
            return R.failed();
        }
        SwStPlanDay swStPlanDay = new SwStPlanDay();
        swStPlanDay.setPlanType(type);
        LambdaQueryWrapper<SwStPlanDay> lambdaQueryWrapper = Wrappers.lambdaQuery(swStPlanDay);
        return R.ok(swStPlanDayService.list(lambdaQueryWrapper));
    }

    @ApiOperation(value = "生产日计划查询", notes = "根据type=2查询生产日计划")
    @GetMapping("/typePlan")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userCode", value = "登录人：当前登录人工号，ES前面没有Q，票务管理系统需要加上前缀Q", dataTypeClass = String.class, required = true),
                    @ApiImplicitParam(name = "taskType", value = "任务类型", dataTypeClass = String.class, required = true),
                    @ApiImplicitParam(name = "issuingMethod", value = "发卡方式", dataTypeClass = String.class, required = false),
                    @ApiImplicitParam(name = "planFinishTime", value = "计划完成时间", dataTypeClass = String.class, required = true),
                    @ApiImplicitParam(name = "producttype", value = "票种（主类型），需要查中间表获取对应的票务系统对应的值，然后和日计划关联查找", dataTypeClass = String.class, required = false),
                    @ApiImplicitParam(name = "productsubtype", value = "票种（子类型），需要查中间表获取对应的票务系统对应的值，然后和日计划关联查找", dataTypeClass = String.class, required = false)
            }
    )
    public R getPPlanDay(
            @RequestParam(value = "userCode", required = true) String userCode,
            @RequestParam(value = "taskType", required = true) String taskType,
            @RequestParam(value = "issuingMethod", required = false) String issuingMethod,
            @RequestParam(value = "planFinishTime", required = true) String planFinishTime,
            @RequestParam(value = "producttype", required = false) String producttype,
            @RequestParam(value = "productsubtype", required = false) String productsubtype
    ) throws ParseException {
        boolean b = StringUtils.isEmpty(userCode) || StringUtils.isEmpty(taskType)
                || StringUtils.isEmpty(planFinishTime);
        if(b){
            return R.failed("检查所传的参数！");
        }
        SwStPlanDay swStPlanDay = new SwStPlanDay();
        if(StringUtils.isNotEmpty(producttype) && StringUtils.isNotEmpty(productsubtype)){
            //根据票种类型和子票种去查找对应票种名称
            //select * from pk_prodparams where producttype = #{producttype} and productSubType = #{productsubtype}
            PkProdparams prodparams = pkProdparamsService.getOne(Wrappers.<PkProdparams>lambdaQuery()
                    .eq(PkProdparams::getProducttype, producttype).eq(PkProdparams::getProductsubtype, productsubtype));
            swStPlanDay.setTicketType(prodparams.getMainName());
            swStPlanDay.setTicketName(prodparams.getSubName());
        }
        //发卡方式可以为null
        if(StringUtils.isNotEmpty(issuingMethod)){
            swStPlanDay.setIssuingMethod(issuingMethod);//发卡方式
        }
        swStPlanDay.setPlanType("2");//2代表生产日计划
        swStPlanDay.setPersonId('Q'+userCode);//车站编码员工号拼接Q
        swStPlanDay.setTaskType(taskType);//任务类型
        swStPlanDay.setPlanFinishTime(DateUtilsSt.DateTurnLocalDateTime(DateUtilsSt.stringToDate(planFinishTime)));//计划完成时间
        swStPlanDay.setEsIsGet("0");//查询es未获取得数据
        LambdaQueryWrapper<SwStPlanDay> lambdaQueryWrapper = Wrappers.lambdaQuery(swStPlanDay);
        //TODO 需要确认是否查询一条数据还是多条数据
        List<SwStPlanDay> list = swStPlanDayService.list(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            for (SwStPlanDay stPlanDay : list) {
                stPlanDay.setPersonId(stPlanDay.getPersonId().substring(1));//返回时员工号去掉Q
                stPlanDay.setPlanFinishDate(stPlanDay.getPlanFinishTime().toLocalDate());//时间转为年-月-日
                if(StringUtils.isNotEmpty(producttype) && StringUtils.isNotEmpty(productsubtype)){
                    stPlanDay.setTicketType(producttype);
                    stPlanDay.setTicketName(productsubtype);
                }
                if("99".equals(stPlanDay.getIssuingMethod())){//99代表没有发卡方式
                    stPlanDay.setIssuingMethod("");
                }
                if(StringUtils.isNotEmpty(stPlanDay.getStartEndNo()) && stPlanDay.getStartEndNo().contains("/")){
                    stPlanDay.setBeginNum(stPlanDay.getStartEndNo().split("/")[0]);
                    stPlanDay.setEndNum(stPlanDay.getStartEndNo().split("/")[1]);
                }else {
                    return R.failed("起止号的格式不正确！");
                }
            }
            return R.ok(list);//成功标记0
        }else {
            return R.failed("未查询到数据信息！");
        }
    }

    @ApiOperation(value = "生产日计划修改", notes = "生产日计划修改")
    @PutMapping("/auditTypePlan")
    public R auditTypePlan(
            @RequestParam(value = "userCode", required = true) String userCode,
            @RequestParam(value = "taskType", required = true) String taskType,
            @RequestParam(value = "issuingMethod", required = false) String issuingMethod,
            @RequestParam(value = "planFinishTime", required = true) String planFinishTime,
            @RequestParam(value = "producttype", required = false) String producttype,
            @RequestParam(value = "productsubtype", required = false) String productsubtype,
            @RequestParam(value = "taskNum", required = true) String taskNum
    ) throws ParseException {
        boolean b = StringUtils.isEmpty(userCode) || StringUtils.isEmpty(taskType)
                || StringUtils.isEmpty(planFinishTime) || StringUtils.isEmpty(taskNum);
        if(b){
            return R.failed("检查所传的参数！");
        }
        SwStPlanDay swStPlanDay = new SwStPlanDay();
        if(StringUtils.isNotEmpty(producttype) && StringUtils.isNotEmpty(productsubtype)){
            //根据票种类型去查找对应得编号
            PkProdparams prodparams = pkProdparamsService.getOne(Wrappers.<PkProdparams>lambdaQuery()
                    .eq(PkProdparams::getProducttype, producttype).eq(PkProdparams::getProductsubtype, productsubtype));
            swStPlanDay.setTicketType(prodparams.getMainName());
            swStPlanDay.setTicketName(prodparams.getSubName());
        }
        //发卡方式可以为null
        if(StringUtils.isNotEmpty(issuingMethod)){
            swStPlanDay.setIssuingMethod(issuingMethod);//发卡方式
        }
        swStPlanDay.setPlanType("2");//2代表生产日计划
        swStPlanDay.setPersonId('Q'+userCode);//车站编码员工号拼接Q
        swStPlanDay.setTaskType(taskType);//任务类型
        swStPlanDay.setPlanFinishTime(DateUtilsSt.DateTurnLocalDateTime(DateUtilsSt.stringToDate(planFinishTime)));//计划完成时间
        swStPlanDay.setEsIsGet("0");//查询es未获取得数据
        LambdaQueryWrapper<SwStPlanDay> lambdaQueryWrapper = Wrappers.lambdaQuery(swStPlanDay);
        //TODO 需要确认是否查询一条数据还是多条数据
        List<SwStPlanDay> list = swStPlanDayService.list(lambdaQueryWrapper);
            if(CollectionUtil.isNotEmpty(list)){
                list.forEach(x -> {
                    x.setEsIsGet("1");//0默认未获取1已获取
                    x.setTaskNum(taskNum);//任务编号
                });
            }else {
                return R.failed("未查询到数据信息！");
            }
        //根据日计划查询的条件，找到该条记录，更新ES是否获取、任务编号（传过来）
        //只是根据id修改，不影响逻辑
        return R.ok(swStPlanDayService.updateBatchById(list));//成功标记0
    }

}
